//
//  CategoryViewController.m
//  Youhuiju
//
//  Created by yutenys on 16/7/27.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "CategoryViewController.h"
//跳转
#import "SearchViewController.h"//搜索界面
#import "MessageViewController.h"//消息界面
#import "FreshProductViewController.h"//全部栏目商品

//自定义view
//#import "YOSearchBar.h"//搜索栏
#import "CategoryTopView.h"//顶上视图


//自定义cell
#import "CategoryTableViewCell.h"//tableviewCell
#import "AdCollectionViewCell.h"//顶上广告cell
#import "CategoryCollectionReusableViews.h"//collectionView Header
#import "CategoryCollectionViewCell.h"//collectionViewCell
@interface CategoryViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

//@property(nonatomic,strong)YOSearchBar *mySearchBar;

@property (weak, nonatomic) IBOutlet UITableView *categoryTableView;
@property (weak, nonatomic) IBOutlet UICollectionView *detailCollectionView;

@property(nonatomic,strong)CategoryTopView *topView;


//判断tableviewCell选中状态
@property(nonatomic,assign)NSInteger falg;

//占位数据
@property(nonatomic,strong) NSArray *data;
@property(nonatomic,strong) NSArray *typeData;

//适配
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewLeading;

@end

@implementation CategoryViewController

static NSString * CategoryTableViewCellIden = @"CategoryTableViewCellIden";

static NSString *AdCollectionViewCellIden  = @"AdCollectionViewCellIden";
static NSString *headerView  = @"headerView";
static NSString *CategoryCollectionViewCellIden  = @"CategoryCollectionViewCellIden";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableViewWidth.constant = 80 * FitW;
    self.collectionViewLeading.constant = 10 * FitW;
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    [self load_UI];
    
     _data = @[@"推荐分类",@"潮流女装",@"时尚男装",@"个护化妆",@"生鲜产品",@"办公用品",@"定制样品"];
    _typeData = @[@"推荐品牌",@"热卖服装",@"新品上市"];
}

#pragma mark ------------- load_UI
-(void)load_UI
{

    
    [self topView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //tableview
    self.categoryTableView.delegate = self;
    self.categoryTableView.dataSource = self;
    [self.categoryTableView registerNib:[UINib nibWithNibName:@"CategoryTableViewCell" bundle:nil] forCellReuseIdentifier:CategoryTableViewCellIden];
    
    //collectionView
    self.detailCollectionView.delegate = self;
    self.detailCollectionView.dataSource  = self;
    [self.detailCollectionView registerNib:[UINib nibWithNibName:@"CategoryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CategoryCollectionViewCellIden];
    [self.detailCollectionView registerNib:[UINib nibWithNibName:@"AdCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:AdCollectionViewCellIden];
    [self.detailCollectionView registerClass:[CategoryCollectionReusableViews class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerView];
//    [self.detailCollectionView registerNib:[UINib nibWithNibName:@"CategoryCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerView];
    
}

#pragma mark ------- tableView delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46 * FitH;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:testTableViewCell forIndexPath:indexPath];
    CategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategoryTableViewCellIden];
    cell.typeString = _data[indexPath.row];
//    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
//     cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
    if ( indexPath.row == self.falg)
    {
        cell.categoryLabel.textColor = [UIColor colorWithHexString:@"ff5f7b"];
        cell.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    }else{
       cell.categoryLabel.textColor = [UIColor colorWithHexString:@"333333"];
        cell.backgroundColor = [UIColor whiteColor];
    }
    

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"%@",_data[indexPath.row]);
    self.falg = indexPath.row;
    [self.categoryTableView reloadData];
}

#pragma mark ------- collectionView delegate
//设置section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}
//设置item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
    return 30;
    }
}
//设置header大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return CGSizeMake(self.detailCollectionView.frame.size.width, 0);
    }
    return CGSizeMake(self.detailCollectionView.frame.size.width, 47 * FitH);
}
//设置headerView
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        CategoryCollectionReusableViews * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerView forIndexPath:indexPath];
        
        //有缓存了
//        [header.imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kDataServer2,_topic_img_url]] placeholderImage:[UIImage imageNamed:@"AppIcon60x60@3x"]];
        if (indexPath.section != 0)
        {
            header.typeLabel.text = _typeData[indexPath.section - 1];
        }
        reusableView = header;
    }
    
    
    return reusableView;
}
//item大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return CGSizeMake(self.detailCollectionView.frame.size.width, 84 * FitH);
    }
    else
    {
    return CGSizeMake(87 * FitW, 107 * FitH);
    }
}

//边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0)
    {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    else
    {
        
        return UIEdgeInsetsMake(12 * FitH, 11 * FitW, 0, 11 * FitW);
    }
}
//载入cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        AdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AdCollectionViewCellIden forIndexPath:indexPath];
        return cell;
    }
    else
    {
        CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CategoryCollectionViewCellIden forIndexPath:indexPath];
//        cell.backgroundColor = [UIColor grayColor];
        return cell;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FreshProductViewController  *DVC = [[FreshProductViewController alloc]initWithNibName:@"FreshProductViewController" bundle:nil];
    DVC.titleString = @"分类下一级";
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:DVC animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
}





#pragma mark ------ private Method
////返回
//-(void)backAction
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}
//
////跳转到搜索界面
//-(void)jumpSearchViewControllerAction:(UIButton *)sender
//{
//    SearchViewController *SVC = [[SearchViewController alloc]init];
//    self.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:SVC animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
//}
//
////跳转到消息界面
//-(void)jumpMessageViewController
//{
//    MessageViewController *MVC = [[MessageViewController alloc]initWithNibName:@"MessageViewController" bundle:nil];
//    self.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:MVC animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
//}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ------- lazy loading
-(CategoryTopView *)topView
{
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[CategoryTopView alloc]init];
    
        _topView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.height.mas_offset(44 );
        }];
        
        _topView.backButtonClickedHandler = ^(NSInteger buttonTag){

            if (buttonTag == 0)
            {
                [weakself.navigationController popViewControllerAnimated:YES];
            }
            else if(buttonTag == 1)
            {
                MessageViewController *MVC = [[MessageViewController alloc]initWithNibName:@"MessageViewController" bundle:nil];
                weakself.hidesBottomBarWhenPushed = YES;
                [weakself.navigationController pushViewController:MVC animated:YES];
                weakself.hidesBottomBarWhenPushed = NO;
            }
            else
            {
                SearchViewController *SVC = [[SearchViewController alloc]init];
                weakself.hidesBottomBarWhenPushed = YES;
                [weakself.navigationController pushViewController:SVC animated:YES];
                weakself.hidesBottomBarWhenPushed = NO;
            }
        };
    }
    return _topView;
}

@end
